#ifndef __TDP2_BYTEORDER_HPP__
#define __TDP2_BYTEORDER_HPP__

#include "../../rdx/src/rdx/rdx_basictypes.hpp"

namespace TDP
{
	namespace ByteOrder
	{
		template<class _T>
		inline _T Swap(const _T &v)
		{
			union
			{
				char bytes[sizeof(_T)];
				_T v;
			} u;
			u.v = v;
			union
			{
				char bytes[sizeof(_T)];
				_T v;
			} us;
			for(LargeUInt i=0;i<sizeof(_T);i++)
				us.bytes[i] = u.bytes[sizeof(_T)-1-i];
			return us.v;
		}

		inline bool SystemIsLittle()
		{
			union
			{
				RDX::UInt32 v;
				RDX::UInt8 b[4];
			} orderCheck;
			orderCheck.v = 1;
			return (orderCheck.b[0] == 1);
		}

		template<class _T>
		inline _T Reorder(const _T &v, bool toBigEndian)
		{
			if(toBigEndian == SystemIsLittle())
				return Swap(v);
			return v;
		}

		template<class _T>
		inline _T Little(const _T &v)
		{
			return Reorder(v, false);
		}

		template<class _T>
		inline _T Big(const _T &v)
		{
			return Reorder(v, true);
		}
	}
}

#endif
